import type { MainResponseType, buyListType } from "@/common/model/response";
import request from "@/utils/axios";

const URL = {
  CHECKUSER: "index/checkUser", //验证用户是否存在
  USERINC: "index/userInc", //用户注册
  INVITEDCODE: "user/getInvitedCode", //获取推广链接
  info: "user/info", //用户信息
  getConfig: "api/getConfig", //系统参数
  getRewardRecord: "user/getRewardRecord", //收益记录
  getNodeArea: "user/getNodeArea", //查询位置
  checkNodeRelation: "user/checkNodeRelation", //查询矩阵
  getTeam: "user/getTeam", //获取下级推荐
  changePayPwd: "user/changePayPwd", //修改支付密码
  getAccountRecord: "user/getAccountRecord", //账户流水
  upgrade: "goods/upgrade", //升级
  getOrderList: "swap/getOrderList", //获取订单列表
  gteService: "api/gteService", //客服
  checkLang: "Languge/checkLang", //检查服务器当前语言
  changeLang: "Languge/changeLang", //切换语言
  getGoodsList: "goods/getGoods", //升级商品列表
  getCheckPwd: "user/checkPwd", //升级商品列表
};

export function checkUser(address: string) {
  return request.post<
    any,
    MainResponseType<{
      _token: string;
    }>
  >(URL.CHECKUSER, {
    address,
  });
}
export function userInc(data: { invite_code: string; address: string }) {
  return request.post<
    any,
    MainResponseType<{
      google_secret: string;
      _token: string;
    }>
  >(URL.USERINC, data);
}
export function getInvitedCode() {
  return request.get<
    any,
    MainResponseType<{
      invite_url: string;
      tgm_url: string;
    }>
  >(URL.INVITEDCODE);
}

export interface BanlanceType {
  account_name: string;
  account_code: string;
  balance: number;
}
interface C2cdata {
  amount_today: number;
  price: number;
  last_matched_time: string;
}
export function userInfo() {
  return request.get<
    any,
    MainResponseType<{
      user_id: number;
      address: string;
      level_name: string;
      paypwd_status: number;
      USDT: BanlanceType;
      XJB: BanlanceType;
      ZCB: BanlanceType;
      JHB: BanlanceType;
      FTB: BanlanceType;
      STF: BanlanceType;
      GBJ: BanlanceType;
      GPB: BanlanceType;
      total_value: number;
      profit_yesterday: number;
      c2c_data: C2cdata;
    }>
  >(URL.info);
}
export function getConfig() {
  return request.get<
    any,
    MainResponseType<{
      recharge: {
        min_rechange_usdt: string;
        max_rechange_usdt: string;
      };
      withdrawal: {
        fee_usdt: string;
        min_withdrawal_usdt: string;
      };
    }>
  >(URL.getConfig);
}
export function getRewardRecord(page: string) {
  return request.get<
    any,
    MainResponseType<
      buyListType<
        {
          add_time: string;
          remark: string;
          order_sn: string;
          reward_amount: number;
          status: number;
          order_id: number;
          reward_type: number;
        }[]
      > & {
        total_reward: number;
      }
    >
  >(URL.getRewardRecord, {
    params: {
      page,
    },
  });
}
export function getNodeArea(node_user_id: string) {
  return request.get<any, MainResponseType<string>>(URL.getNodeArea, {
    params: {
      node_user_id,
    },
  });
}
export function checkNodeRelation(user_id?: string) {
  return request.get<
    any,
    MainResponseType<{
      list_data: {
        user_id: number;
        depth: number;
        button_user_id: string;
        can_bind: boolean;
        children: {
          user_id: number;
          depth: number;
          button_user_id: string;
          can_bind: boolean;
          children: {
            user_id: number;
            depth: number;
            button_user_id: string;
            can_bind: boolean;
          }[];
        }[];
      };
    }>
  >(URL.checkNodeRelation, {
    params: {
      user_id,
    },
  });
}
export function getTeam(data: { user_id: string; page: string }) {
  return request.get<
    any,
    MainResponseType<
      buyListType<
        {
          user_id: number;
          address: string;
          level: number;
          regdate: string;
          invite_number: number;
        }[]
      > & { invite_number: number }
    >
  >(URL.getTeam, {
    params: data,
  });
}
export function changePayPwd(data: {
  google_code: string;
  new_password: string;
  confirm_password: string;
}) {
  return request.post<any, MainResponseType<string>>(URL.changePayPwd, data);
}
export function getAccountRecord(data: { page: string; currency: string }) {
  return request.get<
    any,
    MainResponseType<
      buyListType<
        {
          add_time: string;
          amount: string;
          description: string;
          from_creat_time: string;
          to_pay_time: string;
          num: string;
          state: string;
        }[]
      >
    >
  >(URL.getAccountRecord, {
    params: data,
  });
}

export enum upgradeEnum {
  check = "check",
  buy = "buy",
}
export function upgrade(data: {
  action: upgradeEnum;
  node_user_id?: string;
  password?: string;
  google_code?: string;
  APITOKEN?: string;
}) {
  return data.action == upgradeEnum.buy
    ? request.post<any, MainResponseType<string>>(URL.upgrade, data)
    : request.post<
        any,
        MainResponseType<{
          get_coin: number;
          next_level: string;
          price: number;
          price_jhb: number;
          price_zcb: number;
        }>
      >(URL.upgrade, data);
}
export function getOrderList(page: string) {
  return request.get<
    any,
    MainResponseType<
      buyListType<
        {
          transfer_id: number;
          transfer_sn: string;
          currency_cost: string;
          amount_cost: number;
          currency_buy: string;
          amount_buy: number;
          cost_buy: number;
          buy_cost: number;
          receive_amount: number;
          add_time: string;
          status: number;
          status_name: string;
        }[]
      >
    >
  >(URL.getOrderList, {
    params: {
      page,
    },
  });
}
export function gteService() {
  return request.get<
    any,
    MainResponseType<{
      service1: string;
      service2: string;
    }>
  >(URL.gteService);
}
export function checkLang() {
  return request.get<
    any,
    MainResponseType<{
      lang: string;
    }>
  >(URL.checkLang);
}
export function changeLang(lang: string) {
  return request.get<
    any,
    MainResponseType<{
      lang: string;
      case: string;
    }>
  >(URL.changeLang, {
    params: {
      lang,
    },
  });
}
export function getGoodsList() {
  return request.get<
    any,
    MainResponseType<
      buyListType<{
        goods_id: number;
        price: number;
        title: string;
      }>
    >
  >(URL.getGoodsList);
}
export function getCheckPwd(password: string) {
  return request.get<
    any,
    MainResponseType<{
      data: string;
    }>
  >(URL.getCheckPwd, {
    params: {
      password,
    },
  });
}
